package my_a10bbc.cn.xp.com.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import my_a10bbc.cn.xp.com.bean.IMusicBean;
import sdstcom.lbq.one.R;

/**
 * Created by Administrator on 2017/1/10.
 */
public class IMusicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context context;
    private ArrayList<IMusicBean.DataBean> list;
    private LayoutInflater inflater;

    private RecyclerView rv;

    private MyOnItemclick click;

    public IMusicAdapter(Context context, ArrayList<IMusicBean.DataBean> list) {
        this.context = context;
        this.list = list;
        inflater=LayoutInflater.from(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh=null;
        View view=inflater.inflate(R.layout.music_recycler_item,parent,false);
        vh=new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh= (ViewHolder) holder;
        IMusicBean.DataBean obj=list.get(position);

        vh.tv1.setText(obj.title);
        vh.tv2.setText(obj.author.user_name);
        Picasso.with(context).load(obj.cover).into(vh.iv);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }



    /**********************/
    public interface MyOnItemclick{
        void onItemClick(int index);
    }
    public void setOnItemClick(MyOnItemclick click) {
        this.click = click;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        rv=recyclerView;
    }


    //点击事件
    @Override
    public void onClick(View v) {
        if(click!=null){
            click.onItemClick(rv.getChildAdapterPosition(v));
        }
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv1,tv2;
        private ImageView iv;
        public ViewHolder(View itemView) {
            super(itemView);
            tv1=(TextView)itemView.findViewById(R.id.tv1_music_recycler_item);
            tv2=(TextView)itemView.findViewById(R.id.tv2_music_recycler_item);
            iv=(ImageView)itemView.findViewById(R.id.iv_music_recycler_item);
        }
    }
}
